﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnimEditor.Define;
using AnimEditor.Picture;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using AnimEditor.Tile;

namespace AnimEditor.Anim
{
	public class AnimFrameData : Object
	{
		TileData mTileData = null;
		AnimFrameTreeNode mTreeNode = null;

		public AnimFrameData(TileData tiledata)
		{
			mTileData = tiledata;
		}
		[Category("属性"), Description("帧矩阵")]
		public Rectangle Rect
		{
			get { return mTileData.Rect; }
			set { mTileData.Rect = value; }
		}
		[Browsable(false)]
		public AnimFrameTreeNode TreeNode
		{
			get { return mTreeNode; }
			set { mTreeNode = value; }
		}
		[Browsable(false)]
		public TileData TileData
		{
			get { return mTileData; }
			set { mTileData = value; }
		}
		public bool Write(BinaryWriter writer)
		{
			writer.Write(mTileData.Index);
			return true;
		}
		public static AnimFrameData Read(BinaryReader writer)
		{
			int index = writer.ReadInt32();
			TileData tiledata = TileMgr.Instance.Tile_Sprites[index];
			if (tiledata == null)
				return null;
			AnimFrameData framedata = new AnimFrameData(tiledata);
			return framedata;
		}
		public void Clear()
		{
			mTileData = null;
			mTreeNode = null;
		}
	}

	public class AnimData : Object
	{
	AnimTreeNode mTreeNode = null;
	string mName;
	int mDelayTime;
	List<AnimFrameData> mAnimFrame = new List<AnimFrameData>();

	[Browsable(false)]
	public List<AnimFrameData> AnimFrame
	{
		get { return mAnimFrame; }
	}
	[Browsable(false)]
	public AnimTreeNode TreeNode
	{
		get { return mTreeNode; }
		set { mTreeNode = value; }
	}


	[Category("属性"), Description("名称")]
	public string Name
	{
		get { return mName; }
		set { mName = value; mTreeNode.Text = mName; }
	}
	[Category("属性"),Description("每帧切换时间")]
	public int DelayTime
	{
		get { return mDelayTime; }
		set { mDelayTime = value; }
	}
    
	public AnimFrameData CreateAnimFrame(TileData tiledata)
	{
		AnimFrameData ret = new AnimFrameData(tiledata);
		mAnimFrame.Add(ret);
		return ret;
	}
	public bool Write(BinaryWriter writer)
	{
		writer.Write(mName);
		writer.Write(mDelayTime);
		writer.Write(AnimFrame.Count);
		for (int i = 0; i<AnimFrame.Count;++i )
		{
		AnimFrame[i].Write(writer);
		}
		return true;
	}
	public bool Read(BinaryReader reader)
	{
		mName = reader.ReadString();
		mDelayTime = reader.ReadInt32();
		int count = reader.ReadInt32();
		for (int i = 0; i<count ; ++i )
		{
		AnimFrameData framedata = AnimFrameData.Read(reader);
		mAnimFrame.Add(framedata);
		}
		return true;
	}
	public void Clear()
	{
		for (int i = 0; i<mAnimFrame.Count;++i )
		{
		AnimFrameData frame = mAnimFrame[i];
		frame.Clear();
		}
		mAnimFrame.Clear();
		mTreeNode = null;
	}
	}

	public class AnimMgr : SingleTon<AnimMgr>
	{
	List<AnimData> mAnimData = new List<AnimData>();
    

	public List<AnimData> AnimDataLst
	{
		get { return mAnimData; }
	}

	public AnimData CreateAnimData()
	{
		AnimData data = new AnimData();
		data.DelayTime = 100;
		mAnimData.Add(data);
		return data;
	}
	public bool Write(BinaryWriter writer)
	{
		writer.Write(AnimDataLst.Count);
		for (int i = 0; i<AnimDataLst.Count; ++i )
		{
		AnimData data = AnimDataLst[i];
		data.Write(writer);
		}
		return true;
	}
	public bool Read(BinaryReader reader)
	{
		int animcount = reader.ReadInt32();
		for (int i = 0; i < animcount; ++i)
		{ 
		AnimData data = AnimMgr.Instance.CreateAnimData();
		data.Read(reader);        
		}
		return true;
	}
	public void Clear()
	{
		for (int i = 0; i<AnimDataLst.Count;++i )
		{
		AnimData data = AnimDataLst[i];
		data.Clear();
		}
		AnimDataLst.Clear();
	}
	}

	#region MakeTileData
	[DefaultPropertyAttribute("GridSize_str")]
	public class MakeTileData : Object
	{
		AnimPoint mStartPoint = null;
		AnimSize mTileSize = null;
		AnimSize mGridSize = null;

		public MakeTileData()
		{
			mStartPoint = PropertyMgr.StringToPoint("0,0");
			mGridSize = PropertyMgr.StringToSize("1,1");
			mTileSize = PropertyMgr.StringToSize("0,0");
		}

		[Browsable(false)]
		public AnimPoint StartPoint
		{
			get { return mStartPoint; }
			set { mStartPoint = value; }
		}

		[Browsable(false)]
		public AnimSize GridSize
		{
			get { return mGridSize; }
			set { mGridSize = value; }
		}

		[Browsable(false)]
		public AnimSize TileSize
		{
			get { return mTileSize; }
			set { mTileSize = value; }
		}
		[Category("属性"), Description("起始点")]
		public string 起始点
		{
			get { return PropertyMgr.PointToString(StartPoint); }
			set
			{
				string str = value;
				PropertyMgr.IsValid(ref str, 2, "0,0");
				mStartPoint = PropertyMgr.StringToPoint(str);
			}
		}
		[Category("属性"), Description("(行,列)数")]
		public string 行列数
		{
			get { return PropertyMgr.SizeToString(GridSize); }
			set
			{
				string str = value;
				PropertyMgr.IsValid(ref str, 2, "0,0");
				GridSize = PropertyMgr.StringToSize(str);
				if (GridSize.Width == 0 || GridSize.Height == 0)
					return;
				TileSize = new AnimSize(PicMgr.Instance.NewBitmapSize.Width / GridSize.Width,
					PicMgr.Instance.NewBitmapSize.Height / GridSize.Height);
			}
		}
		[Category("属性"), Description("每个瓦片图的大小")]
		public string 瓦片大小
		{
			get { return PropertyMgr.SizeToString(TileSize); }
			set
			{
				string str = value;
				PropertyMgr.IsValid(ref str, 2, "0,0");
				mTileSize = PropertyMgr.StringToSize(str);
			}
		}
	}
#endregion
}
